package managedbean.Customer; 
 
import entity.CRM.Customer; 
import entity.Employee;
import java.io.IOException;
import java.util.List; 
import javax.ejb.EJB; 
import javax.el.ELContext;
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.Controller;
import session.stateless.CRM.CustomerBeanLocal;
 
@ManagedBean(name="viewCustomerList")
@ViewScoped
public class ViewCustomerManagerBean { 
    @EJB 
    private CustomerBeanLocal customerSessionBeanLocal; 
    private Customer selectedCustomer;
 
    public ViewCustomerManagerBean() { 
    } 
 
 
    public List<Customer> getCustomers() 
    { 
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        Employee executive = controller.getEmployee();
        return customerSessionBeanLocal.getCustomers(executive.getEmployeeId()); 
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
    
    
    public void editCustomer(ActionEvent event) throws IOException
    {
        Customer selectedCustomer = this.getSelectedCustomer();
        if (selectedCustomer != null){
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("editCustomer.xhtml");                
        }
    }
    
    public void deleteCustomer(ActionEvent event) throws IOException
    {
        selectedCustomer = this.getSelectedCustomer();
        try
        {
            customerSessionBeanLocal.deleteCustomer(selectedCustomer.getRegistrationNumber());            
            
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewCustomer.xhtml");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
    }
   
    
    public void viewSaleInquiries(ActionEvent event) throws IOException
    {
        selectedCustomer = this.getSelectedCustomer();
        System.out.println("view Customer: view Sale Inquiries: info customer: "+selectedCustomer.getSaleLeadAddress());
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewCustomersInquiries.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void viewSaleQuotations(ActionEvent event) throws IOException
    {
        selectedCustomer = this.getSelectedCustomer();
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewCustomersQuotations.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void viewSaleOrders(ActionEvent event) throws IOException
    {
        selectedCustomer = this.getSelectedCustomer();
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewCustomersSaleOrders.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
} 
